<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<div contenteditable></div>
<script>
  console.clear();

  var editableNode = document.querySelector('div[contenteditable]');

  function setContent(html) {
    editableNode.innerHTML = html.replace(/\|/g, '<em class="bookmark"></em>');

    var bookmarkNodes=  editableNode.querySelectorAll('em.bookmark');

    if (bookmarkNodes.length) {
      var range = document.createRange();

      range.setStartBefore(bookmarkNodes[0]);
      if (bookmarkNodes.length >= 2) {
        range.setEndAfter(bookmarkNodes[1]);
      }

      Array.prototype.forEach.call(bookmarkNodes, function (bookmarkNode) {
        bookmarkNode.parentNode.removeChild(bookmarkNode);
      });
      var selection = window.getSelection();
      selection.removeAllRanges();
      selection.addRange(range);
    }
  }

  // Begin example code

  editableNode.focus();
  setContent('<p><br>|</p>');

  document.execCommand('bold');
  /**
   * Chrome and Firefox: How is the command state true when nothing in the DOM has changed?
   */
  console.log(document.queryCommandState('bold'), editableNode.innerHTML);

  var selection = document.getSelection();
  var range = selection.getRangeAt(0);
</script>
</body>
</html>
